/**
 * 2018年12月10日下午10:53:29
 * @author 谢进财
 */
package com.baihang.project.ds.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baihang.framework.controller.BaseController;
import com.baihang.framework.kit.database.RedisKit;
import com.baihang.project.ds.entity.ShouCangEntity;
import com.baihang.project.ds.service.ShouCangRedisService;

import redis.clients.jedis.Jedis;

/**
 * 收藏
 *
 */
@Controller
@RequestMapping("/shoucang")
public class ShouCangController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(ShouCangController.class);
	private Jedis jedis = RedisKit.getJedis();
	@Autowired
	//private ShouCangService service;
	private ShouCangRedisService service;
	@RequestMapping
	private String index(Model model) {
		int syNum = service.countSyShouCang();
		List<ShouCangEntity> list = service.getShouCang();
		model.addAttribute("syNum", syNum);
		model.addAttribute("list", list);
		model.addAttribute("StartTime", jedis.hmget("systemConfig","StartTime").get(0));
		return "/project/ds/shoucang/index";
	}
	
	@RequestMapping("/indexData")
	private String indexData(Model model) {
		int syNum = service.countSyShouCang();
		List<ShouCangEntity> list = service.getShouCang();
		model.addAttribute("syNum", syNum);
		model.addAttribute("list", list);
		model.addAttribute("StartTime", jedis.hmget("systemConfig","StartTime").get(0));
		
		return "/project/ds/shoucang/indexData";
	}
	
	@RequestMapping(value ="/doShouCang", produces="application/json;charset=UTF-8")
	@ResponseBody
	private String doShouCang(int id) {
		System.err.println("收藏"+id);
		String msg = service.doShouCang(id);
		String result = "true";
		if(!"收藏成功".equals(msg)) {
			result ="false";
		}
		System.err.println(String.format("{\"result\":\"%s\",\"msg\":\"%s\"}", result,msg));
		return String.format("{\"result\":\"%s\",\"msg\":\"%s\"}", result,msg);
	}
	
	@RequestMapping("/doTop")
	private String doTop(int id,int sort,Model model) {
		service.doTop(id, sort);
		List<ShouCangEntity> list = service.getShouCang();
		model.addAttribute("list", list);
		return "/project/ds/shoucang/shoucangData";
	}
	
	@RequestMapping("/doDown")
	private String doDown(int id,int sort,Model model) {
		service.doDown(id, sort);
		List<ShouCangEntity> list = service.getShouCang();
		model.addAttribute("list", list);
		return "/project/ds/shoucang/shoucangData";
	}
	
	@RequestMapping("/doDel")
	private String doDel(int fangyuanId,int shoucangId,Model model) {
		service.doDel(fangyuanId, shoucangId);
		List<ShouCangEntity> list = service.getShouCang();
		model.addAttribute("list", list);
		return "/project/ds/shoucang/shoucangData";
	}
}
